#!/bin/bash

L4T_TOP=$(dirname $(pwd))
L4T_ROOTFS_IMG=system.img.raw

function check_root()
{
	if [[ $(id -u) -ne 0 ]]; then
		echo -e "\n[ERROR] Please run as root\n"
		exit 1
	fi
}

function check_folder()
{
	if [[ $(basename ${L4T_TOP}) != "Linux_for_Tegra" ]] \
		|| [[ ! $(basename $(pwd)) =~ ^Ritrontek_ ]]; then
		echo -e "\n[ERROR] This BSP should be extracted in the Linux_for_Tegra directory of your Jetpack installed."
		echo -e "          This install script must be run from the Linux_for_Tegra/Ritrontek_xxx directory."
		exit 2
	fi
}

function get_kernel_vermagic()
{
	if [[ $# -le 0 ]]; then
		return
	fi

	local kernel_image_file=$1
	strings ${kernel_image_file} | grep -i "Linux version [0-9]" | awk '{print $3}'
}

function install_kernel_modules()
{
	local rootfs_img=$1
	local kernel_ver=$2

	local file src_file dst_file
	local mnt=/mnt
	declare -a files
	files=(
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/f75308.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/ina2xx.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/hwmon/pwm-fan.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/devices/qspi_mtd.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtd_blkdevs.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtdblock.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/mtd/mtd.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/net/ethernet/realtek/r8168.ko"
		"lib/modules/${kernel_ver}/kernel/drivers/spi/spi-tegra210-qspi.ko"
		"lib/modules/${kernel_ver}/modules.alias"
		"lib/modules/${kernel_ver}/modules.alias.bin"
		"lib/modules/${kernel_ver}/modules.builtin"
		"lib/modules/${kernel_ver}/modules.builtin.alias.bin"
		"lib/modules/${kernel_ver}/modules.builtin.bin"
		"lib/modules/${kernel_ver}/modules.builtin.modinfo"
		"lib/modules/${kernel_ver}/modules.dep"
		"lib/modules/${kernel_ver}/modules.dep.bin"
		"lib/modules/${kernel_ver}/modules.devname"
		"lib/modules/${kernel_ver}/modules.order"
		"lib/modules/${kernel_ver}/modules.softdep"
		"lib/modules/${kernel_ver}/modules.softdep"
		"lib/modules/${kernel_ver}/modules.symbols"
		"lib/modules/${kernel_ver}/modules.symbols.bin"
	)

	mount ${rootfs_img} ${mnt}

	echo -e "[INFO] install kernel modules from ${rootfs_img}:"
	for file in ${files[@]}; do
		src_file=${mnt}/${file}
		dst_file=${L4T_TOP}/rootfs/${file}

		if [[ -f ${dst_file} ]]; then
			continue
		fi

		if [[ -f ${src_file} ]]; then
			if [[ ! -d $(dirname ${dst_file}) ]]; then
				mkdir -p $(dirname ${dst_file})
			fi

			echo -e "[INFO] >>> ${dst_file}"
			cp ${src_file} ${dst_file}
		fi
	done

	umount ${mnt}
}

######################################################

if [[ $# -ge 1 ]]; then
	L4T_ROOTFS_IMG=$1

	if [[ ! -f ${L4T_ROOTFS_IMG} ]]; then
		echo -e "\n[WARN] Rootfs image doesnot exis: ${L4T_ROOTFS_IMG}."
		read -p "Continue ? (y/N)" key

		if [[ ${key} != "Y" && ${key} != "y" ]]; then
			exit 0
		fi
	fi
fi

check_root

check_folder

install_kernel_modules ${L4T_ROOTFS_IMG} $(get_kernel_vermagic resources/kernel/Image)

sync
